<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>登录界面</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        form {
            background-color: #f4f4f4;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            padding: 8px;
            margin-bottom: 10px;
            width: 100%;
            box-sizing: border-box;
            border-radius: 5px;
            border: 1px solid #ddd;
        }

        input[type="submit"] {
            background-color: #4caf50;
            color: white;
            cursor: pointer;
        }

        p {
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <form id="loginForm" onsubmit="return validateLogin()">
        <h1>用户登录</h1>
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" ><br>

        <label for="password">密码:</label>
        <input type="password" id="password" name="password" ><br>

        <input type="submit" value="登录">
        <p id="loginMessage"></p>
    </form>

    <script>
        function validateLogin() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;


            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username: username, password: password })
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        window.location.href = "/stu";
                    } else {
                        document.getElementById("loginMessage").innerText = "登录失败，请检查用户名和密码。";
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });


            return false; // Prevent form submission
        }
    </script>
</body>

</html>